package org.example.simplemanagementsystem.controller;


import org.example.simplemanagementsystem.pojo.*;
import org.example.simplemanagementsystem.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 分页查询
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        PageResult pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 根据id删除相关的班级信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        clazzService.delete(id);
        return Result.success();
    }

    /**
     * 新增班级数据
     * @param clazz
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzService.add(clazz);
        return Result.success();
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        Clazz clazz = clazzService.getClassById(id);
        return Result.success(clazz);
    }

    /**
     * 更细班级数据
     * @param clazz
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 获取所有班级
     * @return
     */
    @GetMapping("/list")
    public Result findAll() {
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }
}



